/*      */ package com.newcrm.db;
/*      */ 
/*      */ import com.newcrm.bean.User;
/*      */ import com.newcrm.exception.DBException;
/*      */ import com.newcrm.exception.IsValidException;
/*      */ import com.newcrm.exception.PwdIsNullException;
/*      */ import com.newcrm.exception.PwdNotMatchException;
/*      */ import com.newcrm.exception.UserIdIsNullException;
/*      */ import com.newcrm.exception.UserIdNotExist;
/*      */ import com.newcrm.util.MD5;
/*      */ import com.newcrm.util.Util;
/*      */ import java.sql.Connection;
/*      */ import java.sql.SQLException;
/*      */ import java.util.ArrayList;
/*      */ import java.util.Date;
/*      */ import java.util.HashMap;
/*      */ import java.util.List;
/*      */ import java.util.Map;
/*      */ import org.apache.commons.dbutils.DbUtils;
/*      */ import org.apache.commons.dbutils.MyMapHandler;
/*      */ import org.apache.commons.dbutils.MyMapListHandler;
/*      */ import org.apache.commons.dbutils.QueryRunner;
/*      */ import org.apache.commons.dbutils.handlers.MapHandler;
/*      */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*      */ import org.apache.commons.lang.StringUtils;
/*      */ import org.apache.log4j.Logger;
/*      */ 
/*      */ public class UserManager
/*      */ {
/*   34 */   private static Logger logger = Logger.getLogger(UserManager.class.getName());
/*      */ 
/*      */   public Integer getLoginNum(String userId)
/*      */     throws DBException, SQLException
/*      */   {
/*   47 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*   48 */     if (conn == null) {
/*   49 */       throw new DBException();
/*      */     }
/*   51 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*   54 */       Map result = (Map)runner.query(conn, 
/*   55 */         "SELECT LoginNum FROM T_User_Info WHERE UserId = ?", new MapHandler(), new Object[] { userId });
/*      */       Integer localInteger;
/*   56 */       if (result == null) {
/*   57 */         return Integer.valueOf(0);
/*      */       }
/*   59 */       return (Integer)result.get("LoginNum");
/*      */     } finally {
/*      */       try {
/*   62 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*   64 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateLoginNum(String userId)
/*      */     throws DBException, SQLException
/*      */   {
/*   78 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*   79 */     if (conn == null) {
/*   80 */       throw new DBException();
/*      */     }
/*   82 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*   85 */       return runner.update(conn, "UPDATE T_User_Info SET LoginNum = LoginNum + 1 WHERE UserId = ?", userId);
/*      */     } finally {
/*      */       try {
/*   88 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*   90 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public Integer getUserCompanyId(String userId)
/*      */     throws DBException, UserIdNotExist, SQLException
/*      */   {
/*  107 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  108 */     if (conn == null) {
/*  109 */       throw new DBException();
/*      */     }
/*  111 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  114 */       Map result = (Map)runner.query(conn, 
/*  115 */         "SELECT A.CompanyId, A.Pwd, B.CompanyName, B.IsValid FROM T_User_Info AS A LEFT JOIN T_Company_Info AS B ON A.CompanyId = B.CompanyId WHERE A.UserId = ?", new MapHandler(), new Object[] { userId });
/*  116 */       if (result == null) {
/*  117 */         throw new UserIdNotExist();
/*      */       }
/*  119 */       return (Integer)result.get("CompanyId");
/*      */     } finally {
/*      */       try {
/*  122 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  124 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public Map<String, Object> login(String userId, String pwd, boolean isEncrypt)
/*      */     throws DBException, UserIdIsNullException, PwdIsNullException, PwdNotMatchException, UserIdNotExist, SQLException, IsValidException
/*      */   {
/*  147 */     if ((userId == null) || (userId.trim().length() == 0)) {
/*  148 */       throw new UserIdIsNullException();
/*      */     }
/*  150 */     if ((pwd == null) || (pwd.trim().length() == 0)) {
/*  151 */       throw new PwdIsNullException();
/*      */     }
/*  153 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  154 */     if (conn == null) {
/*  155 */       throw new DBException();
/*      */     }
/*  157 */     Map map = new HashMap();
/*  158 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  161 */       Map result = (Map)runner.query(conn, 
/*  162 */         "SELECT A.CompanyId, A.Pwd, B.CompanyName, B.IsValid FROM T_User_Info AS A LEFT JOIN T_Company_Info AS B ON A.CompanyId = B.CompanyId WHERE A.UserId = ?", new MapHandler(), new Object[] { userId });
/*  163 */       if (result == null) {
/*  164 */         throw new UserIdNotExist();
/*      */       }
/*      */ 
/*  167 */       String dbPwd = (String)result.get("Pwd");
/*  168 */       if (isEncrypt) {
/*  169 */         if (!pwd.equals(dbPwd)) {
/*  170 */           throw new PwdNotMatchException();
/*      */         }
/*      */       }
/*  173 */       else if (!MD5.getMD5(pwd).equals(dbPwd)) {
/*  174 */         throw new PwdNotMatchException();
/*      */       }
/*      */ 
/*  178 */       Integer isValid = (Integer)result.get("IsValid");
/*  179 */       if ((isValid == null) || (isValid.intValue() == 0)) {
/*  180 */         logger.warn("公司表中，IsValid = 0");
/*  181 */         throw new IsValidException();
/*      */       }
/*      */ 
/*  184 */       map.put("companyId", (Integer)result.get("CompanyId"));
/*  185 */       map.put("companyName", (String)result.get("CompanyName"));
/*  186 */       return map;
/*      */     } finally {
/*      */       try {
/*  189 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  191 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public Map<String, Object> checkSessionKey(String sessionKey)
/*      */     throws DBException, SQLException
/*      */   {
/*  206 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  207 */     if (conn == null) {
/*  208 */       throw new DBException();
/*      */     }
/*  210 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  212 */       return (Map)runner.query(conn, "SELECT CompanyId, UserId FROM T_User_Info WHERE SessionKey = ?", 
/*  213 */         new MapHandler(), new Object[] { sessionKey });
/*      */     } finally {
/*      */       try {
/*  216 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  218 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public Map<String, Object> getUserCompanyInfo(String userId)
/*      */     throws DBException, UserIdNotExist, SQLException, UserIdIsNullException
/*      */   {
/*  240 */     if ((userId == null) || (userId.trim().length() == 0)) {
/*  241 */       throw new UserIdIsNullException();
/*      */     }
/*  243 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  244 */     if (conn == null) {
/*  245 */       throw new DBException();
/*      */     }
/*  247 */     Map map = new HashMap();
/*  248 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  250 */       Map result = (Map)runner.query(conn, 
/*  251 */         "SELECT A.CompanyId, B.CompanyName FROM T_User_Info AS A LEFT JOIN T_Company_Info AS B ON A.CompanyId = B.CompanyId WHERE A.UserId = ?", new MapHandler(), new Object[] { userId });
/*  252 */       if (result == null) {
/*  253 */         throw new UserIdNotExist();
/*      */       }
/*  255 */       map.put("companyId", (Integer)result.get("CompanyId"));
/*  256 */       map.put("companyName", (String)result.get("CompanyName"));
/*  257 */       return map;
/*      */     } finally {
/*      */       try {
/*  260 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  262 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int getCompanyUsersNum(int companyId)
/*      */   {
/*  274 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  275 */     if (conn == null) {
/*  276 */       return 0;
/*      */     }
/*      */ 
/*  279 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  281 */       Object obj = runner.query(conn, "SELECT COUNT(*) FROM T_User_Info WHERE CompanyId = ? ORDER BY GroupId ASC ", 
/*  282 */         new ScalarHandler(1), new Object[] { Integer.valueOf(companyId) });
/*  283 */       if (!(obj instanceof Long)) {
/*  284 */         return 0;
/*      */       }
/*  286 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/*  288 */       logger.warn("get count failed", e);
/*      */     } finally {
/*      */       try {
/*  291 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  293 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  296 */     return 0;
/*      */   }
/*      */ 
/*      */   public List<User> getCompanyUsers(int companyId, String groupIds)
/*      */   {
/*  309 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  310 */     if (conn == null) {
/*  311 */       return null;
/*      */     }
/*      */ 
/*  314 */     QueryRunner runner = new QueryRunner();
/*  315 */     List users = new ArrayList();
/*      */     try {
/*  317 */       List result = null;
/*      */ 
/*  319 */       if ((groupIds != null) && (groupIds.trim().length() > 0)) {
/*  320 */         if (!groupIds.contains(","))
/*      */         {
/*  322 */           result = (List)runner.query(conn, "SELECT A.UserId,A.Name,A.Sex,A.HireDate,A.IdCard,A.Mobile,A.QQ,A.Email,A.IsAdmin,A.IsCreater,A.OperationFlag,A.ImgPath,A.GroupId,A.IsGroupAdmin,B.ParentGroupId FROM T_User_Info AS A LEFT JOIN T_Company_Group_Info AS B ON A.GroupId = B.GroupId WHERE A.CompanyId = ? AND A.GroupId = ? ORDER BY A.IsGroupAdmin DESC, CONVERT(A.Name USING GBK) DESC ", 
/*  323 */             new MyMapListHandler(), new Object[] { Integer.valueOf(companyId), 
/*  324 */             Integer.valueOf(Util.str2Int(groupIds)) });
/*      */         }
/*      */         else
/*  327 */           result = 
/*  328 */             (List)runner
/*  328 */             .query(conn, 
/*  329 */             "SELECT A.UserId,A.Name,A.Sex,A.HireDate,A.IdCard,A.Mobile,A.QQ,A.Email,A.IsAdmin,A.IsCreater,A.OperationFlag,A.ImgPath,A.GroupId,A.IsGroupAdmin,B.ParentGroupId FROM T_User_Info AS A LEFT JOIN T_Company_Group_Info AS B ON A.GroupId = B.GroupId WHERE A.CompanyId = ? AND A.GroupId IN ( " + 
/*  330 */             groupIds + 
/*  331 */             " ) ORDER BY A.IsGroupAdmin DESC, CONVERT(A.Name USING GBK) DESC ", 
/*  332 */             new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*      */       }
/*      */       else {
/*  335 */         result = (List)runner.query(conn, "SELECT A.UserId,A.Name,A.Sex,A.HireDate,A.IdCard,A.Mobile,A.QQ,A.Email,A.IsAdmin,A.IsCreater,A.OperationFlag,A.ImgPath,A.GroupId,A.IsGroupAdmin,B.ParentGroupId FROM T_User_Info AS A LEFT JOIN T_Company_Group_Info AS B ON A.GroupId = B.GroupId WHERE A.CompanyId = ? ORDER BY A.GroupId ASC, A.IsGroupAdmin DESC, CONVERT(A.Name USING GBK) DESC ", 
/*  336 */           new MyMapListHandler(), new Object[] { Integer.valueOf(companyId) });
/*      */       }
/*  338 */       if (result == null) {
/*  339 */         List localList1 = users; return localList1;
/*      */       }
/*  341 */        for (Object itemO : result) {
            Map item = (Map)itemO;
/*  342 */         User user = new User();
/*  343 */         user.companyId = companyId;
/*  344 */         user.userId = ((String)item.get("UserId"));
/*  345 */         user.name = ((String)item.get("Name"));
/*  346 */         user.sex = Util.str2Int(item.get("Sex"));
/*  347 */         user.hireDate = Util.str2Date(item.get("HireDate"));
/*  348 */         user.idCard = ((String)item.get("IdCard"));
/*  349 */         user.mobile = ((String)item.get("Mobile"));
/*  350 */         user.qq = ((String)item.get("QQ"));
/*  351 */         user.email = ((String)item.get("Email"));
/*  352 */         user.imgPath = ((String)item.get("ImgPath"));
/*  353 */         if ((user.imgPath == null) || (user.imgPath.trim().length() == 0))
/*  354 */           user.imgPath = "images/avatar_person.png";
/*      */         else {
/*  356 */           user.imgPath = ("images/upload/identity/" + user.imgPath);
/*      */         }
/*  358 */         user.isAdmin = (Util.str2Int(item.get("IsAdmin")) == 1);
/*      */ 
/*  360 */         user.isCreater = (Util.str2Int(item.get("IsCreater")) == 1);
/*      */ 
/*  363 */         user.groupId = Util.str2Int(item.get("GroupId"));
/*  364 */         user.parentGroupId = Util.str2Int(item.get("ParentGroupId"));
/*  365 */         user.isGroupAdmin = (Util.str2Int(item.get("IsGroupAdmin")) == 1);
/*      */ 
/*  368 */         String operationFlag = (String)item.get("OperationFlag");
/*  369 */         if ((operationFlag != null) && (operationFlag.trim().length() > 0))
/*      */         {
/*  371 */           user.canExport = (Integer.parseInt(Util.getArrayValue(
/*  372 */             operationFlag.split(","), 
/*  373 */             0)) == 1);
/*      */ 
/*  375 */           user.canDelete = (Integer.parseInt(Util.getArrayValue(
/*  376 */             operationFlag.split(","), 
/*  377 */             1)) == 1);
/*      */         }
/*      */ 
/*  381 */         user.extInfo = getExtInfo(conn, user.userId);
/*  382 */         users.add(user);
/*      */       }
/*      */     } catch (SQLException e) {
/*  385 */       logger.warn("SQLException", e);
/*      */       try
/*      */       {
/*  388 */         DbUtils.close(conn);
/*      */       } catch (SQLException se) {
/*  390 */         logger.warn("close conn failed", se);
/*      */       }
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/*  388 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  390 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  393 */     return users;
/*      */   }
/*      */ 
/*      */   public User getUserBasicInfo(String userId)
/*      */     throws DBException
/*      */   {
/*  404 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  405 */     if (conn == null) {
/*  406 */       return null;
/*      */     }
/*  408 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  410 */       Map result = (Map)runner.query(conn, 
/*  411 */         "SELECT UserId,Name,Sex,HireDate,IdCard,Mobile,QQ,Email,IsAdmin,IsCreater,OperationFlag,ImgPath,GroupId,IsGroupAdmin FROM T_User_Info WHERE UserId = ? ", new MyMapHandler(), new Object[] { userId });
/*  412 */       if (result == null) {
/*  413 */         return null;
/*      */       }
/*      */ 
/*  416 */       User user = new User();
/*  417 */       user.userId = ((String)result.get("UserId"));
/*  418 */       user.name = ((String)result.get("Name"));
/*  419 */       user.email = ((String)result.get("Email"));
/*  420 */       user.imgPath = ((String)result.get("ImgPath"));
/*  421 */       user.sex = Util.str2Int(result.get("Sex"));
/*  422 */       user.hireDate = Util.str2Date(result.get("HireDate"));
/*  423 */       user.idCard = ((String)result.get("IdCard"));
/*  424 */       user.mobile = ((String)result.get("Mobile"));
/*  425 */       user.qq = ((String)result.get("QQ"));
/*  426 */       if ((user.imgPath == null) || (user.imgPath.trim().length() == 0))
/*  427 */         user.imgPath = "images/avatar_person.png";
/*      */       else {
/*  429 */         user.imgPath = ("images/upload/identity/" + user.imgPath);
/*      */       }
/*  431 */       user.isAdmin = (Util.str2Int(result.get("IsAdmin")) == 1);
/*      */ 
/*  433 */       user.isCreater = (Util.str2Int(result.get("IsCreater")) == 1);
/*      */ 
/*  436 */       user.groupId = Util.str2Int(result.get("GroupId"));
/*  437 */       user.isGroupAdmin = (Util.str2Int(result.get("IsGroupAdmin")) == 1);
/*      */ 
/*  440 */       String operationFlag = (String)result.get("OperationFlag");
/*  441 */       if ((operationFlag != null) && (operationFlag.trim().length() > 0))
/*      */       {
/*  443 */         user.canExport = (Integer.parseInt(Util.getArrayValue(
/*  444 */           operationFlag.split(","), 
/*  445 */           0)) == 1);
/*      */ 
/*  447 */         user.canDelete = (Integer.parseInt(Util.getArrayValue(
/*  448 */           operationFlag.split(","), 
/*  449 */           1)) == 1);
/*      */       }
/*      */ 
/*  454 */       user.extInfo = getExtInfo(conn, user.userId);
/*  455 */       return user;
/*      */     } catch (SQLException e) {
/*  457 */       logger.warn("SQLException", e);
/*  458 */       throw new DBException();
/*      */     } finally {
/*      */       try {
/*  461 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  463 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int add(String id, String name, int groupId, int isGroupAdmin, int sex, Date hireDate, String idCard, String mobile, String qq, String email, String imgPath, String password, int companyId)
/*      */     throws SQLException, DBException
/*      */   {
/*  487 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  488 */     if (conn == null) {
/*  489 */       throw new DBException();
/*      */     }
/*  491 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  494 */       return runner.update(conn, "INSERT INTO T_User_Info (UserId,CompanyId,Pwd,Name,Sex,HireDate,IdCard,Mobile,QQ,Email,ImgPath,OperationFlag,IsAdmin,IsCreater,LoginNum,CreateDate,UpdateDate,GroupId,IsGroupAdmin) VALUES (?,?,?,?,?,?,?,?,?,?,?,'0,0',0,0,0,NOW(),NOW(),?,?)", new Object[] { id, Integer.valueOf(companyId), 
/*  495 */         MD5.getMD5(password), name, Integer.valueOf(sex), hireDate, idCard, mobile, 
/*  496 */         qq, email, imgPath, Integer.valueOf(groupId), Integer.valueOf(isGroupAdmin) });
/*      */     } finally {
/*      */       try {
/*  499 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  501 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int addAdmin(String id, String name, String email, String qq, String mobile, String imgPath, String password, int companyId)
/*      */     throws SQLException, DBException
/*      */   {
/*  522 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  523 */     if (conn == null) {
/*  524 */       throw new DBException();
/*      */     }
/*  526 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  529 */       return runner.update(conn, "INSERT INTO T_User_Info (UserId,CompanyId,Pwd,Name,Email,QQ,Mobile,ImgPath,OperationFlag,IsAdmin,IsCreater,LoginNum,CreateDate,UpdateDate) VALUES (?,?,?,?,?,?,?,?,'1,1',1,1,0,NOW(),NOW())", new Object[] { id, Integer.valueOf(companyId), 
/*  530 */         password, name, email, qq, mobile, imgPath });
/*      */     } finally {
/*      */       try {
/*  533 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  535 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int update(String id, String name, int sex, String idCard, String mobile, String qq, String email, String imgPath, String password)
/*      */     throws DBException, SQLException
/*      */   {
/*  558 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  559 */     if (conn == null) {
/*  560 */       throw new DBException();
/*      */     }
/*  562 */     QueryRunner runner = new QueryRunner();
/*  563 */     String sql = "UPDATE T_User_Info SET Name = ?, Sex = ?, IdCard = ?, Mobile = ?, QQ = ?, Email = ? ";
/*  564 */     List params = new ArrayList();
/*  565 */     params.add(name);
/*  566 */     params.add(Integer.valueOf(sex));
/*  567 */     params.add(idCard);
/*  568 */     params.add(mobile);
/*  569 */     params.add(qq);
/*  570 */     params.add(email);
/*  571 */     if ((password != null) && (password.trim().length() > 0)) {
/*  572 */       sql = sql + ", Pwd = ?";
/*  573 */       params.add(MD5.getMD5(password));
/*      */     }
/*  575 */     if ((imgPath != null) && (imgPath.trim().length() > 0)) {
/*  576 */       sql = sql + ", ImgPath = ?";
/*  577 */       params.add(imgPath);
/*      */     }
/*  579 */     sql = sql + " WHERE UserId = ?";
/*  580 */     params.add(id);
/*      */     try {
/*  582 */       return runner.update(conn, sql, params.toArray());
/*      */     } finally {
/*      */       try {
/*  585 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  587 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateAvatar(String id, String imgPath)
/*      */     throws DBException, SQLException
/*      */   {
/*  603 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  604 */     if (conn == null) {
/*  605 */       throw new DBException();
/*      */     }
/*  607 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  609 */       return runner.update(conn, 
/*  610 */         "UPDATE T_User_Info SET ImgPath = ? WHERE UserId = ? ", new Object[] { 
/*  611 */         imgPath, id });
/*      */     } finally {
/*      */       try {
/*  614 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  616 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updatePwd(String id, String pwd)
/*      */     throws DBException, SQLException
/*      */   {
/*  632 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  633 */     if (conn == null) {
/*  634 */       throw new DBException();
/*      */     }
/*  636 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/*  638 */       return runner.update(conn, 
/*  639 */         "UPDATE T_User_Info SET Pwd = ? WHERE UserId = ? ", new Object[] { 
/*  640 */         MD5.getMD5(pwd), id });
/*      */     } finally {
/*      */       try {
/*  643 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  645 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateIsAdmin(String id, int isAdmin)
/*      */     throws DBException, SQLException
/*      */   {
/*  661 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  662 */     if (conn == null) {
/*  663 */       throw new DBException();
/*      */     }
/*  665 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  668 */       String sql = "UPDATE T_User_Info SET IsAdmin = ? WHERE UserId = ?";
/*  669 */       return runner.update(conn, sql, new Object[] { Integer.valueOf(isAdmin), id });
/*      */     } finally {
/*      */       try {
/*  672 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  674 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updatePassword(String id, String password)
/*      */     throws DBException, SQLException
/*      */   {
/*  690 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  691 */     if (conn == null) {
/*  692 */       throw new DBException();
/*      */     }
/*  694 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  697 */       String sql = "UPDATE T_User_Info SET Pwd = ? WHERE UserId = ?";
/*  698 */       return runner.update(conn, sql, new Object[] { MD5.getMD5(password), id });
/*      */     } finally {
/*      */       try {
/*  701 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  703 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateGroup(String id, int groupId, int isGroupAdmin)
/*      */     throws DBException, SQLException
/*      */   {
/*  720 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  721 */     if (conn == null) {
/*  722 */       throw new DBException();
/*      */     }
/*  724 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  727 */       String sql = "UPDATE T_User_Info SET GroupId = ?, IsGroupAdmin = ? WHERE UserId = ?";
/*  728 */       return runner.update(conn, sql, new Object[] { Integer.valueOf(groupId), Integer.valueOf(isGroupAdmin), id });
/*      */     } finally {
/*      */       try {
/*  731 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  733 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateOperationFlag(String id, String flag)
/*      */     throws DBException, SQLException
/*      */   {
/*  749 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  750 */     if (conn == null) {
/*  751 */       throw new DBException();
/*      */     }
/*  753 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  756 */       String sql = "UPDATE T_User_Info SET OperationFlag = ? WHERE UserId = ?";
/*  757 */       return runner.update(conn, sql, new Object[] { flag, id });
/*      */     } finally {
/*      */       try {
/*  760 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  762 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public void modifyOwner(String oldOwner, String newOwner)
/*      */     throws DBException, SQLException
/*      */   {
/*  777 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  778 */     if (conn == null) {
/*  779 */       throw new DBException();
/*      */     }
/*  781 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  785 */       String sql = "UPDATE T_Customer_Info SET PermissionsId = ?, IndexFlag = 0 WHERE PermissionsId = ?";
/*  786 */       runner.update(conn, sql, new Object[] { newOwner, oldOwner });
/*      */ 
/*  788 */       sql = "UPDATE T_Deal_Info SET ResponsibleUserId = ? WHERE ResponsibleUserId = ?";
/*  789 */       runner.update(conn, sql, new Object[] { newOwner, oldOwner });
/*      */ 
/*  792 */       runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", oldOwner);
/*      */     } finally {
/*      */       try {
/*  795 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  797 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int delete(String id)
/*      */   {
/*  811 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  812 */     if (conn == null) {
/*  813 */       return 0;
/*      */     }
/*  815 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  818 */       String sql = "DELETE FROM T_User_Info WHERE UserId = ?";
/*  819 */       int result = runner.update(conn, sql, id);
/*      */ 
/*  822 */       sql = "DELETE FROM T_Task_Info WHERE CreateUserId = ? OR ResponsibleUserId = ? ";
/*  823 */       runner.update(conn, sql, new Object[] { id, id });
/*      */ 
/*  826 */       sql = "DELETE FROM T_User_VOIP WHERE UserId = ? ";
/*  827 */       runner.update(conn, sql, id);
/*      */ 
/*  829 */       sql = "DELETE FROM T_User_Ext_Info WHERE UserId = ? ";
/*  830 */       runner.update(conn, sql, id);
/*      */ 
/*  832 */       sql = "DELETE FROM T_User_Email WHERE UserId = ? ";
/*  833 */       runner.update(conn, sql, id);
/*      */ 
/*  836 */       sql = "UPDATE T_Customer_Info SET PermissionsId = ?, UpdateDate = NOW() WHERE PermissionsId = ?";
/*  837 */       runner.update(conn, sql, new Object[] { "admin", id });
/*      */ 
/*  844 */       runner.update(conn, "DELETE FROM T_Customer_Permissions_Info WHERE CustomerId = ? ", id);
/*      */ 
/*  846 */       return result;
/*      */     } catch (SQLException e) {
/*  848 */       logger.warn("delete user failed", e);
/*      */     } finally {
/*      */       try {
/*  851 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  853 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */ 
/*  857 */     return 0;
/*      */   }
/*      */ 
/*      */   public boolean isUserIdExist(String userId)
/*      */   {
/*  867 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  868 */     if (conn == null) {
/*  869 */       return false;
/*      */     }
/*  871 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  874 */       Map result = (Map)runner.query(conn, 
/*  875 */         "SELECT UserId FROM T_User_Info WHERE UserId = ?", new MapHandler(), new Object[] { userId });
/*  876 */       if ((result == null) || (result.size() == 0))
/*  877 */         return false;
/*      */     } catch (SQLException localSQLException1) {
/*      */     }
/*      */     finally {
/*      */       try {
/*  882 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  884 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */     try
/*      */     {
/*  882 */       DbUtils.close(conn);
/*      */     } catch (SQLException e) {
/*  884 */       logger.warn("close conn failed", e);
/*      */     }
/*      */ 
/*  887 */     return true;
/*      */   }
/*      */ 
/*      */   public int updateSessionKey(String userId, String ip, String sessionKey)
/*      */     throws DBException, SQLException
/*      */   {
/*  903 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  904 */     if (conn == null) {
/*  905 */       throw new DBException();
/*      */     }
/*  907 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  910 */       return runner.update(conn, "UPDATE T_User_Info SET LastLoginDate = NOW(), LoginNum = LoginNum + 1, LastLoginIP = ?, SessionKey = ? WHERE UserId = ?", new Object[] { ip, 
/*  911 */         sessionKey, userId });
/*      */     } finally {
/*      */       try {
/*  914 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  916 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateLogin(String userId, String ip)
/*      */     throws DBException, SQLException
/*      */   {
/*  932 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  933 */     if (conn == null) {
/*  934 */       throw new DBException();
/*      */     }
/*  936 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/*  939 */       return runner.update(conn, "UPDATE T_User_Info SET LastLoginDate = NOW(), LoginNum = LoginNum + 1, LastLoginIP = ? WHERE UserId = ?", new Object[] { ip, userId });
/*      */     } finally {
/*      */       try {
/*  942 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  944 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public int updateUsersExtInfo(String ids, int fieldType, String value)
/*      */   {
/*  959 */     if ((ids == null) || (ids.length() == 0)) {
/*  960 */       return 0;
/*      */     }
/*  962 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  963 */     if (conn == null) {
/*  964 */       return 0;
/*      */     }
/*  966 */     QueryRunner runner = new QueryRunner();
/*  967 */     int num = 0;
/*      */     try {
/*  969 */       for (String id : ids.split(";")) {
/*  970 */         String sql = "UPDATE T_User_Ext_Info SET FieldValue = ? WHERE UserId = ? AND FieldType = ?";
/*  971 */         int result = runner.update(conn, sql, new Object[] { value, id, Integer.valueOf(fieldType) });
/*  972 */         if (result == 0) {
/*  973 */           sql = "INSERT INTO T_User_Ext_Info (UserId, FieldType, FieldValue) values (?,?,?)";
/*  974 */           runner.update(conn, sql, new Object[] { id, Integer.valueOf(fieldType), value });
/*      */         }
/*  976 */         num++;
/*      */       }
/*      */     }
/*      */     catch (SQLException e) {
/*  980 */       logger.warn("update user extinfo failed", e);
/*      */       try
/*      */       {
/*  983 */         DbUtils.close(conn);
/*      */       } catch (SQLException se) {
/*  985 */         logger.warn("close conn failed", se);
/*      */       }
/*      */     }
/*      */     finally
/*      */     {
/*      */       try
/*      */       {
/*  983 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/*  985 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*  988 */     return num;
/*      */   }
/*      */ 
/*      */   public int updateExtInfo(String id, int fieldType, String value)
/*      */     throws DBException, SQLException
/*      */   {
/* 1003 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1004 */     if (conn == null) {
/* 1005 */       throw new DBException();
/*      */     }
/* 1007 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 1010 */       String sql = "UPDATE T_User_Ext_Info SET FieldValue = ? WHERE UserId = ? AND FieldType = ?";
/* 1011 */       int result = runner.update(conn, sql, new Object[] { value, id, Integer.valueOf(fieldType) });
/*      */       int i;
/* 1012 */       if (result > 0) {
/* 1013 */         return result;
/*      */       }
/*      */ 
/* 1016 */       sql = "INSERT INTO T_User_Ext_Info (UserId, FieldType, FieldValue) values (?,?,?)";
/* 1017 */       return runner.update(conn, sql, new Object[] { id, Integer.valueOf(fieldType), value });
/*      */     } finally {
/*      */       try {
/* 1020 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1022 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public String getExtInfoByType(String id, int type)
/*      */     throws DBException, SQLException
/*      */   {
/* 1038 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1039 */     if (conn == null) {
/* 1040 */       throw new DBException();
/*      */     }
/* 1042 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 1045 */       Map result = (Map)runner.query(conn, 
/* 1046 */         "SELECT FieldValue FROM T_User_Ext_Info WHERE UserId = ? AND FieldType = ?", new MapHandler(), new Object[] { id, Integer.valueOf(type) });
/* 1047 */       if (result == null) {
/* 1048 */         return null;
/*      */       }
/* 1050 */       return (String)result.get("FieldValue");
/*      */     } finally {
/*      */       try {
/* 1053 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1055 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */   }
/*      */ 
/*      */   public Map<Integer, String> getExtInfo(Connection conn, String id)
/*      */     throws SQLException
/*      */   {
/* 1072 */     QueryRunner runner = new QueryRunner();
/*      */ 
/* 1074 */     String sql = "SELECT FieldType, FieldValue FROM T_User_Ext_Info WHERE UserId = ?";
/* 1075 */     List result = (List)runner.query(conn, sql, 
/* 1076 */       new MyMapListHandler(), new Object[] { id });
/* 1077 */     if (result == null) {
/* 1078 */       return null;
/*      */     }
/*      */ 
/* 1081 */     Map extInfos = new HashMap();
/* 1082 */      for (Object itemO : result) {
        Map item = (Map)itemO;
/* 1083 */       extInfos.put((Integer)item.get("FieldType"), 
/* 1084 */         (String)item.get("FieldValue"));
/*      */     }
/*      */ 
/* 1087 */     return extInfos;
/*      */   }
/*      */ 
/*      */   public boolean isInstalled()
/*      */   {
/* 1096 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 1097 */     if (conn == null) {
/* 1098 */       return false;
/*      */     }
/* 1100 */     QueryRunner runner = new QueryRunner();
/*      */     try {
/* 1102 */       Object obj = runner.query(conn, "SELECT COUNT(*) FROM T_User_Info", 
/* 1103 */         new ScalarHandler(1));
/* 1104 */       if (!(obj instanceof Long)) {
/* 1105 */         return false;
/*      */       }
/* 1107 */       int num = ((Long)obj).intValue();
/* 1108 */       if (num > 0)
/* 1109 */         return true;
/*      */     }
/*      */     catch (SQLException e) {
/* 1112 */       logger.warn("isInstalled failed", e);
/*      */     } finally {
/*      */       try {
/* 1115 */         DbUtils.close(conn);
/*      */       } catch (SQLException e) {
/* 1117 */         logger.warn("close conn failed", e);
/*      */       }
/*      */     }
/*      */     try
/*      */     {
/* 1115 */       DbUtils.close(conn);
/*      */     } catch (SQLException e) {
/* 1117 */       logger.warn("close conn failed", e);
/*      */     }
/*      */ 
/* 1120 */     return false;
/*      */   }
/*      */ 
/*      */   public void addViewCustomerId(String userId, String customerId)
/*      */     throws DBException, SQLException
/*      */   {
/* 1133 */     if ((customerId == null) || (customerId.trim().length() == 0)) {
/* 1134 */       return;
/*      */     }
/* 1136 */     String ids = getExtInfoByType(userId, 
/* 1137 */       3);
/*      */ 
/* 1139 */     List list = new ArrayList();
/* 1140 */     if ((ids == null) || (ids.trim().length() == 0)) {
/* 1141 */       list.add(customerId);
/*      */     } else {
/* 1143 */       for (String id : ids.split(","))
/* 1144 */         if (!customerId.equals(id))
/*      */         {
/* 1147 */           list.add(id);
/*      */         }
/* 1149 */       if (list.size() > 10) {
/* 1150 */         list.remove(0);
/*      */       }
/* 1152 */       list.add(customerId);
/*      */     }
/* 1154 */     updateExtInfo(userId, 3, 
/* 1155 */       StringUtils.join(list.toArray(), ","));
/*      */   }
/*      */ 
/*      */   public long getTotalNum(Connection conn, int companyId)
/*      */   {
/* 1166 */     if (conn == null) {
/* 1167 */       return 0L;
/*      */     }
/* 1169 */     QueryRunner runner = new QueryRunner();
/*      */     try
/*      */     {
/* 1172 */       Object obj = runner.query(conn, 
/* 1173 */         "SELECT COUNT(*) FROM T_User_Info WHERE CompanyId = ? ", 
/* 1174 */         new ScalarHandler(1), new Object[] { Integer.valueOf(companyId) });
/* 1175 */       if (!(obj instanceof Long)) {
/* 1176 */         return 0L;
/*      */       }
/* 1178 */       return ((Long)obj).intValue();
/*      */     } catch (SQLException e) {
/* 1180 */       logger.warn("get user total num failed", e);
/*      */     }
/* 1182 */     return 0L;
/*      */   }
/*      */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.UserManager
 * JD-Core Version:    0.6.1
 */